package org.hu.controller;

import org.hu.model.Employee;
import org.hu.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * 查询员工
 * @author Htrees
 */
@Controller
@RequestMapping("/admin")
public class SearchemployeesController {

    @Autowired
    EmployeeService employeeService;

    public static final Integer PAGE_SIZE = 10;

    /**
     * 查找员工
     * @param employee 员工对象
     * @param page 页数
     * @param model
     * @return
     */
    @RequestMapping("/searchemployees")
    public String getAllEmps(Employee employee, @RequestParam(defaultValue = "1") Integer page, Model model){
        List<Employee> emps = employeeService.getAllEmployees(employee,page,PAGE_SIZE);
        Long total = employeeService.getTotal(employee);
        model.addAttribute("emps",emps);
        model.addAttribute("total",total);
        model.addAttribute("page",page);
        model.addAttribute("pagenum",total % PAGE_SIZE==0 ? total / PAGE_SIZE : total / PAGE_SIZE+1);
        return "searchemployees";
    }

    /**
     * 关闭账户
     * @param employeeid
     * @return searchemployees
     */
    @RequestMapping("/banUser")
    public String updateempstatus(Integer employeeid){
        employeeService.updateStatus(employeeid, 2);
        return "redirect:/admin/searchemployees?status=1";
    }
}
